package com.zeng.sim.controller;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.sim.controller.domain.SimMsgCodeMapping;
import com.zeng.sim.controller.domain.request.PageDomain;
import com.zeng.sim.controller.domain.response.Result;
import com.zeng.sim.controller.domain.response.ResultTable;
import com.zeng.sim.mapper.model.SimMsgCodeMappingEntity;
import com.zeng.sim.mapper.model.SimMsgRecordEntity;
import com.zeng.sim.service.SimMsgCodeMappingService;
import com.zeng.sim.service.SimMsgRecordService;
import com.zeng.sim.util.SimConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zengwanlin
 * @since 2023-12-27
 */
@Controller
@RequestMapping("/sim/msgCode/mapping")
public class SimMsgCodeMappingController {

    @Autowired
    private SimMsgCodeMappingService simMsgCodeMappingService;

    @Autowired
    private SimMsgRecordService simMsgRecordService;

    @GetMapping("/list")
    public String main() {
        return "sim/msgCode/mapping/main";
    }

    @GetMapping("/add")
    public String add() {
        return "sim/msgCode/mapping/add";
    }

    @GetMapping("/edit")
    public String edit(Model model, Integer id) {
        model.addAttribute("msgCodeMapping", simMsgCodeMappingService.getById(id));
        return "sim/msgCode/mapping/edit";
    }

    @GetMapping("/data")
    @ResponseBody
    public ResultTable data(SimMsgCodeMapping msgCodeMapping, PageDomain pageDomain) {
        Page<SimMsgCodeMapping> page = simMsgCodeMappingService.data(msgCodeMapping, pageDomain);
        return ResultTable.pageTable(page);
    }

    @DeleteMapping("/remove/{id}")
    @ResponseBody
    public Result remove(@PathVariable Integer id) {
        return Result.decide(simMsgCodeMappingService.removeById(id));
    }

    @DeleteMapping("/batchRemove/{ids}")
    @ResponseBody
    public Result batchRemove(@PathVariable String ids) {
        List<Integer> idList = ListUtil.toList(ids.split(","))
            .stream()
            .mapToInt(Integer::parseInt)
            .boxed()
            .collect(Collectors.toList());
        return Result.decide(simMsgCodeMappingService.removeBatchByIds(idList));
    }

    @PostMapping("/save")
    @ResponseBody
    public Result save(@RequestBody SimMsgCodeMapping msgCodeMapping) {
        // 检查当前请求报文是否已存在映射关系
        LambdaQueryWrapper<SimMsgCodeMappingEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SimMsgCodeMappingEntity::getReqChannelNo, msgCodeMapping.getReqChannelNo());
        wrapper.eq(SimMsgCodeMappingEntity::getReqMsgCode, msgCodeMapping.getReqMsgCode());
        if (simMsgCodeMappingService.exists(wrapper)) {
            throw new IllegalArgumentException("当前请求报文已存在映射关系");
        }
        // 检查当前响应报文是否未配置
        LambdaQueryWrapper<SimMsgRecordEntity> msgRecordWrapper = new LambdaQueryWrapper<>();
        msgRecordWrapper.eq(SimMsgRecordEntity::getBusiType, SimConstant.BUSI_TYPE_02);
        msgRecordWrapper.eq(SimMsgRecordEntity::getChannelNo, msgCodeMapping.getRespChannelNo());
        msgRecordWrapper.eq(SimMsgRecordEntity::getMsgCode, msgCodeMapping.getRespMsgCode());
        if (!simMsgRecordService.exists(msgRecordWrapper)) {
            throw new IllegalArgumentException("当前响应报文不存在，请先配置服务端报文");
        }

        SimMsgCodeMappingEntity msgCodeMappingEntity = new SimMsgCodeMappingEntity();
        BeanUtils.copyProperties(msgCodeMapping, msgCodeMappingEntity);
        return Result.decide(simMsgCodeMappingService.save(msgCodeMappingEntity));
    }

    @PutMapping("/update")
    @ResponseBody
    public Result update(@RequestBody SimMsgCodeMapping msgCodeMapping) {
        SimMsgCodeMappingEntity msgCodeMappingEntity = new SimMsgCodeMappingEntity();
        BeanUtils.copyProperties(msgCodeMapping, msgCodeMappingEntity);
        return Result.decide(simMsgCodeMappingService.updateById(msgCodeMappingEntity));
    }

}
